using System;
using System.Collections.Generic;
using OracleInternal.Common;

namespace OracleInternal.TTC.Accessors
{
	internal class TTCNamedTypeAccessor : Accessor
	{
		internal const int CharacterSetId = 2;

		internal string m_typeName;

		internal TTCNamedTypeAccessor(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine, bool bForBind, string typeName)
			: base(colMetaData, marshallingEngine, bForBind)
		{
			m_typeName = typeName;
			if (m_totalLengthOfData == null)
			{
				m_totalLengthOfData = new List<int>();
			}
		}

		internal void UnmarshalColumnData(int dataLength)
		{
			m_marshallingEngine.m_oraBufRdr.m_bParsingColumnData = true;
			m_marshallingEngine.m_oraBufRdr.m_bMarkStartOffsetForColData = true;
			try
			{
				m_marshallingEngine.UnmarshalCLR_ColData(dataLength);
			}
			finally
			{
				m_marshallingEngine.m_oraBufRdr.m_bParsingColumnData = false;
				m_marshallingEngine.m_oraBufRdr.m_bMarkStartOffsetForColData = false;
			}
		}

		internal override void UnmarshalColumnData()
		{
			int num = 0;
			if (!m_bNullByDescribe)
			{
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				num = (int)m_marshallingEngine.UnmarshalUB4();
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				if (num > 0)
				{
					UnmarshalColumnData(num);
				}
				else
				{
					m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[m_marshallingEngine.m_oraBufRdr.m_colDataStartOffsetIndexToUpdate] = -1;
				}
			}
			else
			{
				m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[m_marshallingEngine.m_oraBufRdr.m_colDataStartOffsetIndexToUpdate] = -1;
			}
			if (m_totalLengthOfData.Count > m_lastRowProcessed)
			{
				m_totalLengthOfData[m_lastRowProcessed] = num;
			}
			else
			{
				m_totalLengthOfData.Add(num);
			}
			m_lastRowProcessed++;
		}

		internal override bool UnmarshalOneRow()
		{
			bool flag = true;
			List<ArraySegment<byte>> dataSegments = null;
			int length = 0;
			if (!m_bNullByDescribe)
			{
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				int num = (int)m_marshallingEngine.UnmarshalUB4();
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				try
				{
					if (num > 0)
					{
						flag = m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(m_RowDataSegments, m_lastRowProcessed);
						m_marshallingEngine.UnmarshalCLR_ScanOnly(num, out dataSegments, ref length);
					}
				}
				finally
				{
					if (num > 0)
					{
						m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					}
				}
				if (m_bForBind && -1 == m_marshallingEngine.ProcessIndicator(length <= 0, length))
				{
					length = 0;
				}
			}
			if (flag)
			{
				m_RowDataSegments.Add(dataSegments);
				m_totalLengthOfData.Add(length);
			}
			else
			{
				m_RowDataSegments[m_lastRowProcessed] = dataSegments;
				m_totalLengthOfData[m_lastRowProcessed] = length;
			}
			m_lastRowProcessed++;
			return false;
		}

		internal override bool IsNullIndicatorSet(int currentRow)
		{
			return m_totalLengthOfData[currentRow] == 0;
		}
	}
}
